<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>API: element   Attribute.js  (YUI Library)</title>
	<link rel="stylesheet" type="text/css" href="assets/api.css">
</head>

<body id="yahoo-com">
<div id="doc3" class="yui-t2">

	<div id="hd">
        <h1>Yahoo! UI Library</h1>
        <h3>element&nbsp; <span class="subtitle">2.2.2</span></h3>
        <p>
        <a href="./index.html">Yahoo! UI Library</a> 
            &gt; <a href="./module_element.html">element</a>
                
                 &gt; Attribute.js (source view) 
            </p>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">

                    <div id="srcout">
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * Provides Attribute configurations.</span>
<span class="c"> * @namespace YAHOO.util</span>
<span class="c"> * @class Attribute</span>
<span class="c"> * @constructor</span>
<span class="c"> * @param hash {Object} The intial Attribute.</span>
<span class="c"> * @param {YAHOO.util.AttributeProvider} The owner of the Attribute instance.</span>
<span class="c"> */</span>

<span class="nx">YAHOO</span><span class="o">.</span><span class="nx">util</span><span class="o">.</span><span class="nx">Attribute</span> <span class="o">=</span> <span class="k">function</span><span class="o">(</span><span class="nx">hash</span><span class="o">,</span> <span class="nx">owner</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">(</span><span class="nx">owner</span><span class="o">)</span> <span class="o">{</span> 
        <span class="k">this</span><span class="o">.</span><span class="nx">owner</span> <span class="o">=</span> <span class="nx">owner</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="nx">configure</span><span class="o">(</span><span class="nx">hash</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">};</span>

<span class="nx">YAHOO</span><span class="o">.</span><span class="nx">util</span><span class="o">.</span><span class="nx">Attribute</span><span class="o">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="o">{</span>
	<span class="c">/**</span>
<span class="c">     * The name of the attribute.</span>
<span class="c">	 * @property name</span>
<span class="c">	 * @type String</span>
<span class="c">	 */</span>
    <span class="nx">name</span><span class="o">:</span> <span class="kc">undefined</span><span class="o">,</span>
    
	<span class="c">/**</span>
<span class="c">     * The value of the attribute.</span>
<span class="c">	 * @property value</span>
<span class="c">	 * @type String</span>
<span class="c">	 */</span>
    <span class="nx">value</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
    
	<span class="c">/**</span>
<span class="c">     * The owner of the attribute.</span>
<span class="c">	 * @property owner</span>
<span class="c">	 * @type YAHOO.util.AttributeProvider</span>
<span class="c">	 */</span>
    <span class="nx">owner</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
    
	<span class="c">/**</span>
<span class="c">     * Whether or not the attribute is read only.</span>
<span class="c">	 * @property readOnly</span>
<span class="c">	 * @type Boolean</span>
<span class="c">	 */</span>
    <span class="nx">readOnly</span><span class="o">:</span> <span class="kc">false</span><span class="o">,</span>
    
	<span class="c">/**</span>
<span class="c">     * Whether or not the attribute can only be written once.</span>
<span class="c">	 * @property writeOnce</span>
<span class="c">	 * @type Boolean</span>
<span class="c">	 */</span>
    <span class="nx">writeOnce</span><span class="o">:</span> <span class="kc">false</span><span class="o">,</span>

	<span class="c">/**</span>
<span class="c">     * The attribute&#39;s initial configuration.</span>
<span class="c">     * @private</span>
<span class="c">	 * @property _initialConfig</span>
<span class="c">	 * @type Object</span>
<span class="c">	 */</span>
    <span class="nx">_initialConfig</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
    
	<span class="c">/**</span>
<span class="c">     * Whether or not the attribute&#39;s value has been set.</span>
<span class="c">     * @private</span>
<span class="c">	 * @property _written</span>
<span class="c">	 * @type Boolean</span>
<span class="c">	 */</span>
    <span class="nx">_written</span><span class="o">:</span> <span class="kc">false</span><span class="o">,</span>
    
	<span class="c">/**</span>
<span class="c">     * The method to use when setting the attribute&#39;s value.</span>
<span class="c">     * The method recieves the new value as the only argument.</span>
<span class="c">	 * @property method</span>
<span class="c">	 * @type Function</span>
<span class="c">	 */</span>
    <span class="nx">method</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
    
	<span class="c">/**</span>
<span class="c">     * The validator to use when setting the attribute&#39;s value.</span>
<span class="c">	 * @property validator</span>
<span class="c">	 * @type Function</span>
<span class="c">     * @return Boolean</span>
<span class="c">	 */</span>
    <span class="nx">validator</span><span class="o">:</span> <span class="kc">null</span><span class="o">,</span>
    
    <span class="c">/**</span>
<span class="c">     * Retrieves the current value of the attribute.</span>
<span class="c">     * @method getValue</span>
<span class="c">     * @return {any} The current value of the attribute.</span>
<span class="c">     */</span>
    <span class="nx">getValue</span><span class="o">:</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">value</span><span class="o">;</span>
    <span class="o">},</span>
    
    <span class="c">/**</span>
<span class="c">     * Sets the value of the attribute and fires beforeChange and change events.</span>
<span class="c">     * @method setValue</span>
<span class="c">     * @param {Any} value The value to apply to the attribute.</span>
<span class="c">     * @param {Boolean} silent If true the change events will not be fired.</span>
<span class="c">     * @return {Boolean} Whether or not the value was set.</span>
<span class="c">     */</span>
    <span class="nx">setValue</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">value</span><span class="o">,</span> <span class="nx">silent</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">beforeRetVal</span><span class="o">;</span>
        <span class="k">var</span> <span class="nx">owner</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">owner</span><span class="o">;</span>
        <span class="k">var</span> <span class="nx">name</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">name</span><span class="o">;</span>
        
        <span class="k">var</span> <span class="nx">event</span> <span class="o">=</span> <span class="o">{</span>
            <span class="nx">type</span><span class="o">:</span> <span class="nx">name</span><span class="o">,</span> 
            <span class="nx">prevValue</span><span class="o">:</span> <span class="k">this</span><span class="o">.</span><span class="nx">getValue</span><span class="o">(),</span>
            <span class="nx">newValue</span><span class="o">:</span> <span class="nx">value</span>
        <span class="o">};</span>
        
        <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">readOnly</span> <span class="o">||</span> <span class="o">(</span> <span class="k">this</span><span class="o">.</span><span class="nx">writeOnce</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="o">.</span><span class="nx">_written</span><span class="o">)</span> <span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="kc">false</span><span class="o">;</span> <span class="c">// write not allowed</span>
<span class="c"></span>        <span class="o">}</span>
        
        <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">validator</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="k">this</span><span class="o">.</span><span class="nx">validator</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">owner</span><span class="o">,</span> <span class="nx">value</span><span class="o">)</span> <span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="kc">false</span><span class="o">;</span> <span class="c">// invalid value</span>
<span class="c"></span>        <span class="o">}</span>

        <span class="k">if</span> <span class="o">(!</span><span class="nx">silent</span><span class="o">)</span> <span class="o">{</span>
            <span class="nx">beforeRetVal</span> <span class="o">=</span> <span class="nx">owner</span><span class="o">.</span><span class="nx">fireBeforeChangeEvent</span><span class="o">(</span><span class="nx">event</span><span class="o">);</span>
            <span class="k">if</span> <span class="o">(</span><span class="nx">beforeRetVal</span> <span class="o">===</span> <span class="kc">false</span><span class="o">)</span> <span class="o">{</span>
                <span class="nx">YAHOO</span><span class="o">.</span><span class="nx">log</span><span class="o">(</span><span class="s1">&#39;setValue &#39;</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> 
                        <span class="s1">&#39;cancelled by beforeChange event&#39;</span><span class="o">,</span> <span class="s1">&#39;info&#39;</span><span class="o">,</span> <span class="s1">&#39;Attribute&#39;</span><span class="o">);</span>
                <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
            <span class="o">}</span>
        <span class="o">}</span>

        <span class="k">if</span> <span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">method</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">this</span><span class="o">.</span><span class="nx">method</span><span class="o">.</span><span class="nx">call</span><span class="o">(</span><span class="nx">owner</span><span class="o">,</span> <span class="nx">value</span><span class="o">);</span>
        <span class="o">}</span>
        
        <span class="k">this</span><span class="o">.</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">value</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="nx">_written</span> <span class="o">=</span> <span class="kc">true</span><span class="o">;</span>
        
        <span class="nx">event</span><span class="o">.</span><span class="nx">type</span> <span class="o">=</span> <span class="nx">name</span><span class="o">;</span>
        
        <span class="k">if</span> <span class="o">(!</span><span class="nx">silent</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">this</span><span class="o">.</span><span class="nx">owner</span><span class="o">.</span><span class="nx">fireChangeEvent</span><span class="o">(</span><span class="nx">event</span><span class="o">);</span>
        <span class="o">}</span>
        
        <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
    <span class="o">},</span>
    
    <span class="c">/**</span>
<span class="c">     * Allows for configuring the Attribute&#39;s properties.</span>
<span class="c">     * @method configure</span>
<span class="c">     * @param {Object} map A key-value map of Attribute properties.</span>
<span class="c">     * @param {Boolean} init Whether or not this should become the initial config.</span>
<span class="c">     */</span>
    <span class="nx">configure</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">map</span><span class="o">,</span> <span class="nx">init</span><span class="o">)</span> <span class="o">{</span>
        <span class="nx">map</span> <span class="o">=</span> <span class="nx">map</span> <span class="o">||</span> <span class="o">{};</span>
        <span class="k">this</span><span class="o">.</span><span class="nx">_written</span> <span class="o">=</span> <span class="kc">false</span><span class="o">;</span> <span class="c">// reset writeOnce</span>
<span class="c"></span>        <span class="k">this</span><span class="o">.</span><span class="nx">_initialConfig</span> <span class="o">=</span> <span class="k">this</span><span class="o">.</span><span class="nx">_initialConfig</span> <span class="o">||</span> <span class="o">{};</span>
        
        <span class="k">for</span> <span class="o">(</span><span class="k">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">map</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">if</span> <span class="o">(</span> <span class="nx">key</span> <span class="o">&amp;&amp;</span> <span class="nx">YAHOO</span><span class="o">.</span><span class="nx">lang</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">map</span><span class="o">,</span> <span class="nx">key</span><span class="o">)</span> <span class="o">)</span> <span class="o">{</span>
                <span class="k">this</span><span class="o">[</span><span class="nx">key</span><span class="o">]</span> <span class="o">=</span> <span class="nx">map</span><span class="o">[</span><span class="nx">key</span><span class="o">];</span>
                <span class="k">if</span> <span class="o">(</span><span class="nx">init</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">this</span><span class="o">.</span><span class="nx">_initialConfig</span><span class="o">[</span><span class="nx">key</span><span class="o">]</span> <span class="o">=</span> <span class="nx">map</span><span class="o">[</span><span class="nx">key</span><span class="o">];</span>
                <span class="o">}</span>
            <span class="o">}</span>
        <span class="o">}</span>
    <span class="o">},</span>
    
    <span class="c">/**</span>
<span class="c">     * Resets the value to the initial config value.</span>
<span class="c">     * @method resetValue</span>
<span class="c">     * @return {Boolean} Whether or not the value was set.</span>
<span class="c">     */</span>
    <span class="nx">resetValue</span><span class="o">:</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="nx">setValue</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">_initialConfig</span><span class="o">.</span><span class="nx">value</span><span class="o">);</span>
    <span class="o">},</span>
    
    <span class="c">/**</span>
<span class="c">     * Resets the attribute config to the initial config state.</span>
<span class="c">     * @method resetConfig</span>
<span class="c">     */</span>
    <span class="nx">resetConfig</span><span class="o">:</span> <span class="k">function</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="nx">configure</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">_initialConfig</span><span class="o">);</span>
    <span class="o">},</span>
    
    <span class="c">/**</span>
<span class="c">     * Resets the value to the current value.</span>
<span class="c">     * Useful when values may have gotten out of sync with actual properties.</span>
<span class="c">     * @method refresh</span>
<span class="c">     * @return {Boolean} Whether or not the value was set.</span>
<span class="c">     */</span>
    <span class="nx">refresh</span><span class="o">:</span> <span class="k">function</span><span class="o">(</span><span class="nx">silent</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="nx">setValue</span><span class="o">(</span><span class="k">this</span><span class="o">.</span><span class="nx">value</span><span class="o">,</span> <span class="nx">silent</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">};</span>
</pre></div>
                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div class="module">
                        <h4>Modules</h4>
                        <ul class="content">

                                <li class=""><a href="module_animation.html">animation</a></li>

                                <li class=""><a href="module_autocomplete.html">autocomplete</a></li>

                                <li class=""><a href="module_button.html">button</a></li>

                                <li class=""><a href="module_calendar.html">calendar</a></li>

                                <li class=""><a href="module_connection.html">connection</a></li>

                                <li class=""><a href="module_container.html">container</a></li>

                                <li class=""><a href="module_datasource.html">datasource</a></li>

                                <li class=""><a href="module_datatable.html">datatable</a></li>

                                <li class=""><a href="module_dom.html">dom</a></li>

                                <li class=""><a href="module_dragdrop.html">dragdrop</a></li>

                                <li class="selected"><a href="module_element.html">element</a></li>

                                <li class=""><a href="module_event.html">event</a></li>

                                <li class=""><a href="module_history.html">history</a></li>

                                <li class=""><a href="module_logger.html">logger</a></li>

                                <li class=""><a href="module_menu.html">menu</a></li>

                                <li class=""><a href="module_slider.html">slider</a></li>

                                <li class=""><a href="module_tabview.html">tabview</a></li>

                                <li class=""><a href="module_treeview.html">treeview</a></li>

                                <li class=""><a href="module_yahoo.html">yahoo</a></li>
                        </ul>
                    </div>

                    <div class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="YAHOO.util.Attribute.html">YAHOO.util.Attribute</a></li>
                                <li class=""><a href="YAHOO.util.AttributeProvider.html">YAHOO.util.AttributeProvider</a></li>
                                <li class=""><a href="YAHOO.util.Element.html">YAHOO.util.Element</a></li>
                        </ul>
                    </div>

                    <div class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class="selected"><a href="Attribute.js.html">Attribute.js</a></li>
                                <li class=""><a href="AttributeProvider.js.html">AttributeProvider.js</a></li>
                                <li class=""><a href="Element.js.html">Element.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2007 Yahoo! Inc. All rights reserved.
	</div>
</div>
</body>
</html>
